<?php
/**
 * @version		$Id$
 * @package		Joomla
 * @subpackage	Content
 * @author		gabe@fijiwebdesign.com
 * @copyright	Copyright (C) 2008 - 2010 Fiji Web Design. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

/**
 * Displays the author's photo in articles
 * 
 * Supported components:
 * 		com_contacts (Default Joomla contacts)
 * To do:
 * 		com_community (JomSocial)
 * 		com_comprofiler (Community Builder)
 *
 * @package		Joomla
 * @subpackage	Content
 * @since 		1.5
 */
class plgContentAuthorphoto extends JPlugin
{

	/**
	 * Constructor
	 *
	 * For php4 compatability we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @param object $subject The object to observe
	 * @param object $params  The object that holds the plugin parameters
	 * @since 1.5
	 */
	function plgContentExample( &$subject, $params )
	{
		parent::__construct( $subject, $params );
	}
	
	/**
	 * Retrieve the Plugin Parameters
	 *
	 * @return JParameter
	 */
	function &getParams() {
		static $params;
		if (!$params) {
			$plugin =& JPluginHelper::getPlugin('content', 'authorphoto');
			$params = new JParameter( $plugin->params );
		}
		return $params;
	}
	
	/**
	 * HTML styling of photo
	 *
	 * @param Object $article
	 * @return String
	 */
	function getPhotoHTML(&$article) {
		if ($article->author_photo) {
			return '
			<p class="author_photo">
				<img src="images/stories/'.$article->author_photo.'" alt="'.$article->author_name.'" />
			</p>
			';
		}
	}

	/**
	 * Display photo on prepare content method
	 *
	 * Method is called by the view
	 *
	 * @param 	object		The article object.  Note $article->text is also available
	 * @param 	object		The article params
	 * @param 	int			The 'page' number
	 */
	function onPrepareContent( &$article, &$params, $limitstart )
	{
		global $mainframe;
		$Db =& JFactory::getDBO();
		
		$userid = $article->created_by;
		$article->author_photo = '';
		if ($userid) {
			$query = "SELECT name, image from  #__contact_details WHERE user_id = ".intval($userid)." AND published = 1 LIMIT 1";
			$Db->setQuery($query);
			if ($row = $Db->loadObject()) {
				$article->author_photo = $row->image;
				$article->author_name = $row->name;
			}
		}
		
		$_params =& $this->getParams();
		if ($_params->get('on_prepare')) {
			echo $this->getPhotoHTML($article);
		}
	
	}

	/**
	 * Display photo on after display title method
	 *
	 * Method is called by the view and the results are imploded and displayed in a placeholder
	 *
	 * @param 	object		The article object.  Note $article->text is also available
	 * @param 	object		The article params
	 * @param 	int			The 'page' number
	 * @return	string
	 */
	function onAfterDisplayTitle( $article, $params, $limitstart )
	{
		global $mainframe;
		
		$_params = $this->getParams();
		if ($_params->get('after_title')) {
			return $this->getPhotoHTML($article);
		}
		
		return '';
	}

	/**
	 * Display photo on before display content method
	 *
	 * Method is called by the view and the results are imploded and displayed in a placeholder
	 *
	 * @param 	object		The article object.  Note $article->text is also available
	 * @param 	object		The article params
	 * @param 	int			The 'page' number
	 * @return	string
	 */
	function onBeforeDisplayContent( $article, $params, $limitstart )
	{
		global $mainframe;
		
		$_params = $this->getParams();
		if ($_params->get('before_content')) {
			return $this->getPhotoHTML($article);
		}
		
		return '';
	}

	/**
	 * Display photo on after display content method
	 *
	 * Method is called by the view and the results are imploded and displayed in a placeholder
	 *
	 * @param 	object		The article object.  Note $article->text is also available
	 * @param 	object		The article params
	 * @param 	int			The 'page' number
	 * @return	string
	 */
	function onAfterDisplayContent( $article, $params, $limitstart )
	{
		global $mainframe;
		
		$_params = $this->getParams();
		if ($_params->get('after_content')) {
			return $this->getPhotoHTML($article);
		}

		return '';
	}

}
